/**
 * Created by JOKER on 2017/7/20.
 */
app.controller('GeneratorWizardCtrl', GeneratorWizardCtrl);
GeneratorWizardCtrl.$inject = ['$scope', '$q', '$http', 'ngTableService', '$filter', 'dbprofileService'];
function GeneratorWizardCtrl($scope, $q, $http, ngTableService, $filter, dbprofileService) {
    var vm = this;
    vm.init = init;
    vm.execute = execute;
    vm.tableFieldInfo = tableFieldInfo;
    vm.config = {
        projectDir: "F:/Mybatis-test-generator",
        modelPackage: "com.csii.cms.service.company.vo",
        domainObjectName: "VoCompany",
        clientPackage: "com.csii.cms.service.company.dao",
        mapPackage: "com.csii.cms.service.company.dao.sqlmap"
    };
    function init() {
        dbprofileService.findAll().then(function (response) {
            $scope.groups = response.data;
        })
    }

    $scope.filter = '';

    $scope.createGroup = function () {
        var group = {name: 'New Group'};
        group.name = $scope.checkItem(group, $scope.groups, 'name');
        $scope.groups.push(group);
    };

    $scope.checkItem = function (obj, arr, key) {
        var i = 0;
        angular.forEach(arr, function (item) {
            if (item[key].indexOf(obj[key]) == 0) {
                var j = item[key].replace(obj[key], '').trim();
                if (j) {
                    i = Math.max(i, parseInt(j) + 1);
                } else {
                    i = 1;
                }
            }
        });
        return obj[key] + (i ? ' ' + i : '');
    };

    $scope.selectGroup = function (item) {
        angular.forEach($scope.groups, function (item) {
            item.selected = false;
        });
        $scope.group = item;
        $scope.group.selected = true;
        $scope.filter = item.name;
        $http({
            method: 'post',
            url: '/loadTable/findAllTables.do',
            data: {"id": item.id}
        }).then(function successCallback(response) {
            $scope.items = response.data.data;
        }, function errorCallback(response) {
        });
    };

    $scope.selectItem = function (configObj) {
        vm.config.tableName = configObj.tableName;
        vm.id = configObj.id;
    };

    $scope.deleteItem = function (item) {
        $scope.items.splice($scope.items.indexOf(item), 1);
        $scope.item = $filter('orderBy')($scope.items, 'first')[0];
        if ($scope.item) $scope.item.selected = true;
    };

    $scope.createItem = function () {
        var item = {
            group: 'Friends',
            avatar: 'img/a0.jpg'
        };
        $scope.items.push(item);
        $scope.selectItem(item);
        $scope.item.editing = true;
    };

    function execute() {
        vm.config.id = $scope.items.id;
        $http({
            method: 'post',
            url: '/loadTable/execute.do',
            data: {
                "generatorConfigDTO" : vm.config,
                "id": vm.id
            }
        }).then(function successCallback(response) {

        }, function errorCallback(response) {
        });
    }

    function tableFieldInfo() {
        dbprofileService.findTableFields(vm.id,vm.config.tableName).then(function (response) {

        })
    }
}